package com.qian.qblog.controller;

import com.qian.qblog.common.BaseResponse;
import com.qian.qblog.common.ErrorCode;
import com.qian.qblog.common.ResultUtils;
import com.qian.qblog.common.exception.BusinessException;
import com.qian.qblog.model.dto.UserRegisterRequest;
import com.qian.qblog.model.entity.User;
import com.qian.qblog.service.UserService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Resource
    private UserService userService;

    @PostMapping("/register")
    public BaseResponse<Long> userRegister(@RequestBody UserRegisterRequest userRegisterRequest) {
        if (userRegisterRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        String userAccount = userRegisterRequest.getUserAccount();
        String userPassword = userRegisterRequest.getUserPassword();
        String checkPassword = userRegisterRequest.getCheckPassword();
        if (StringUtils.isAnyBlank(userAccount, userPassword, checkPassword)) {
            return null;
        }
        long result = userService.userRegister(userAccount, userPassword, checkPassword);
        return ResultUtils.success(result);
    }

    @GetMapping("/test")
    public BaseResponse<User> getTest() {
        return ResultUtils.success(userService.getTest());
    }
}
